public class Balancedbinary {
    public static void main(String[] args) {
         int[] arr={10,11,7,6,8,9};
        AVLtree avLtree = new AVLtree();
        for (int i = 0; i < arr.length; i++) {
            avLtree.add(new Node(arr[i]));
        }

        System.out.println("左支:"+avLtree.lefthight());
        System.out.println("右支:"+avLtree.righthight());
        System.out.println("最长的一支:"+avLtree.hight());
        System.out.println(avLtree.getRoot().left);
    }
}

class AVLtree{
    private Node root;

    public Node getRoot() {
        return root;
    }

    public void add(Node node) {//增加结点
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }
    public void Midorder(){//中序遍历
        if (root != null) {
            root.Midorder();
        } else {
            System.out.println("null");
        }
    }

    public int lefthight(){
        if (root!=null){
            return root.lefthight();
        }else {
            return 0;
        }
    }
    public int righthight(){
        if (root!=null){
            return root.righthight();
        }else {
            return 0;
        }
    }
    public int hight(){
        if (root!=null){
            return root.hight();
        }else {
            return 0;
        }
    }
}


class Node{
    public int value;
    public Node left;
    public Node right;

    public Node(int value) {
        super();
        this.value = value;
    }


    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    public int lefthight(){//左子树结点数
        if (left==null){
            return 0;
        }
        return left.hight();
    }
    public int righthight(){//右子树结点数
        if (right==null){
            return 0;
        }
        return right.hight();
    }
    public int hight(){//二叉树最长结点数
        return Math.max(left==null? 0 :left.hight() , right==null?0: right.hight())+1;
    }
    public void left(){//左旋转
        Node node=new Node(value);
        node.left=left;
        node.right=right.left;
        value=right.value;
        right=right.right;
        left=node;
    }
    public void right(){//右旋转
        Node node=new Node(value);
        node.right=right;
        node.left=left.right;
        value=left.value;
        left=left.left;
        right=node;
    }
    public void Midorder(){//中序遍历
        if (this.left!=null){
            this.left.Midorder();
        }
        System.out.println(this);
        if (this.right!=null){
            this.right.Midorder();
        }
    }


    public void add(Node node) {//增加结点
        if (this.value > node.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        }
        if (this.value < node.value) {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
        if (righthight()-lefthight()>1){//右大于左--》左旋转
            if (right!=null&&right.righthight()<right.lefthight()){//右子树的左节点大于它的右节点--》右旋转右子树
                right.right();
                left();
            }else {
                left();
            }
        }
        if (lefthight()-righthight()>1){////右小于左--》右旋转
            if (left!=null&&left.righthight()>left.lefthight()){//左子树的右节点大于它的左节点--》左旋转左子树
                left.left();
                right();
            }else {
                right();
            }
        }
    }

}
